package com.cuihanyu.newuniversityover.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.cuihanyu.newuniversityover.R;
import com.cuihanyu.newuniversityover.util.SPUtils;

import java.util.ArrayList;

/**
 * Created by Administrator on 2017/5/15 0015.
 */

public class GuidanceActivity extends AppCompatActivity {
    private ArrayList<ImageView> imageList;
    private static final int[] images = new int[]{
            R.drawable.login1, R.drawable.login2
    };
    private ViewPager viewPager;
    private Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题
        setContentView(R.layout.viewpager);
        button = findViewById(R.id.button);
        viewPager = findViewById(R.id.viewPager);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SPUtils.put(GuidanceActivity.this, "is_user_guide_showed", true);
                Intent intent = new Intent(GuidanceActivity.this, MainActivity.class);
                startActivity(intent);
                finish();
            }
        });
        //viewpager 的滑动监听
        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            //滑动事件
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            //选中事件
            public void onPageSelected(int position) {
                if (position == images.length - 1) {// 最后一个页面
                    button.setVisibility(View.VISIBLE);// 显示开始体验的按钮
                } else {
                    button.setVisibility(View.INVISIBLE);
                }
            }

            @Override
            //滑动状态发生变化
            public void onPageScrollStateChanged(int state) {

            }
        });
        initViews();
        PagerAdapter pagerAdapter = new PagerAdapter() {
            @Override
            public int getCount() {
                return images.length;
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(imageList.get(position));
            }

            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(imageList.get(position));
                return imageList.get(position);
            }
        };
        viewPager.setAdapter(pagerAdapter);
    }

    private void initViews() {
        imageList = new ArrayList<ImageView>();
        //初始化三个界面
        for (int i = 0; i < images.length; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setBackgroundResource(images[i]);//添加引导页背景
            imageList.add(imageView);
        }
    }
}
